package com.base.git;

import org.eclipse.jgit.api.Git;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


@Service
public class GitCommandService {

    private static final String remoteUrl = "https://gitee.com/xuyou551/testGitCommand.git";
    private static final String username = "179647302@qq.com";
    private static final String password = "xu123123";
    private static final String localPath = "D:\\test\\git\\";
    private static final String gitFilePath = "/src/main/java/com/demo/json/";
    private static final String tempCreateLocalPath = "D:\\test\\";

    public void createJsonFile(String branch,String json) {
        String createLocalPath = tempCreateLocalPath + UUID.randomUUID().toString();
        try {
            GitCommandUtils.deleteFolder(new File(createLocalPath));
            GitCommandUtils.getGit(remoteUrl, branch, username, password, createLocalPath).close();
            String filePath = createLocalPath + gitFilePath ;
            createFile(filePath, json);
            GitCommandUtils.commit(createLocalPath,"xuyou123","179647302@qq.com","test");
            GitCommandUtils.push(remoteUrl, username, password, createLocalPath);
            GitCommandUtils.deleteFolder(new File(createLocalPath));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    public List<String> getFileNameList(String branch) throws Exception {
        pull(branch);
        String filePath = localPath + gitFilePath;
        File file = new File(filePath);
        List<String> fileNameList = new ArrayList<>();
        if (file.exists() && file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    fileNameList.add(f.getName());
                }
            }
        }
        GitCommandUtils.deleteFolder(new File(localPath));
      return fileNameList;
    }

    public String getFileContent(String branch,String fileName) throws Exception {
        String content = "";
        pull(branch);
        String filePath = localPath + gitFilePath + fileName;
        File file = new File(filePath);
        if (file.exists()) {
            // 使用 BufferedReader 读取文件
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            StringBuilder sb = new StringBuilder();
            String line;
            // 逐行读取文件内容并添加到 StringBuilder 中
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            // 关闭 reader
            reader.close();
            content = sb.toString();
        }
        GitCommandUtils.deleteFolder(new File(localPath));
        return content;
    }


    public List<String> getBranchList() throws Exception {
        List<String> branchList = GitCommandUtils.getBranchList(remoteUrl, username,
                password, localPath);
        return branchList;
    }

    public List<String> getHistoryList(String branch) throws Exception {
        return GitCommandUtils.getHistoryList(remoteUrl,username, password, localPath,branch);
    }

    public List<HistoryResponse> getHistoryListByFileName(String branch,String fileName) throws Exception {
        return GitCommandUtils.getHistoryListByFileName(remoteUrl,username, password, localPath,branch,fileName);
    }

    public List<String> getFilePathListByCommitId(String branch,String commitId) throws Exception {
        return GitCommandUtils.getFilePathListByCommitId(remoteUrl,username, password, localPath,branch,commitId);
    }

    public void pull(String branch) throws Exception {
        GitCommandUtils.deleteFolder(new File(localPath));
        Git git = GitCommandUtils.getGit(remoteUrl, branch, username, password, localPath);
        git.close();
    }

    private String createFile(String filePath ,String json) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String fileName = filePath + UUID.randomUUID().toString()+ ".json";
        FileOutputStream fos = new FileOutputStream(fileName);
        OutputStreamWriter writer = new OutputStreamWriter(fos, "UTF-8");
        BufferedWriter bw = new BufferedWriter(writer);
        bw.write(json);
        bw.flush();
        return fileName;
    }

    public void updateFileContent(UpdateFileContentRequest request) throws Exception {
        String createLocalPath = tempCreateLocalPath + UUID.randomUUID().toString();
        GitCommandUtils.deleteFolder(new File(createLocalPath));
        Git git = GitCommandUtils.getGit(remoteUrl, request.getBranch(), username, password, createLocalPath);
        git.close();
        String filePathNew = createLocalPath + request.getFilePath() + request.getFileName();
        File file = new File(filePathNew);
        if (!file.exists()) {
            file.mkdir();
        }
        FileOutputStream fos = new FileOutputStream(filePathNew);
        OutputStreamWriter writer = new OutputStreamWriter(fos, "UTF-8");
        BufferedWriter bw = new BufferedWriter(writer);
        bw.write(request.getJson());
        bw.flush();
        GitCommandUtils.commit(createLocalPath,request.getAuthorName(),request.getAuthorEmail(),request.getMessage());
        GitCommandUtils.push(remoteUrl, username, password, createLocalPath);
        GitCommandUtils.deleteFolder(new File(createLocalPath));
    }



}
